/*
*Copyright (C) 2007-2011 Borqs Ltd. All rights reserved.
*/

package com.borqs.music.lyric;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import android.graphics.Paint;

public class LyricUtils {
    
    public static int indexOf(byte[] data, byte b, int start, int length) {
        int end = Math.min(start + length, data.length);
        for (int i = start; i < end; i++) {
            if (data[i] == b) {
                return i - start;
            }
        }

        return -1;
    }

    /**
     * Get UTF8 bytes from a string
     *
     * @param string
     * @return UTF8 byte array, or null if failed to get UTF8 byte array
     */
    public static byte[] getUTF8Bytes(String string) {
        if (string == null)
            return new byte[0];

        try {
            return string.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            /*
             * If system doesn't support UTF-8, use another way
             */

            try {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                DataOutputStream dos = new DataOutputStream(bos);
                dos.writeUTF(string);
                byte[] jdata = bos.toByteArray();
                bos.close();
                dos.close();
                byte[] buff = new byte[jdata.length - 2];
                System.arraycopy(jdata, 2, buff, 0, buff.length);
                return buff;
            } catch (IOException ex) {
                return new byte[0];
            }
        }
    }

    /**
     * Get string in UTF-8 encoding
     */
    public static String getUTF8String(byte[] b, int start, int length) {
        if (b == null) {
            return "";
        } else {
            try {
                return new String(b, start, length, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                return "";
            }
        }
    }

    /**
     * Parse int value from string
     *
     * @param radix the radix to use when parsing.
     * @param value the string representation of a long value.
     * @param parse failed will return value
     *
     * @return int  the primitive int value represented by string using radix.
     */
    public static int getInt(int radix, String value, int faultValue) {
        if (value == null) {
            return faultValue;
        }

        try {
            return (int) Integer.parseInt(value.trim(), radix);
        } catch (NumberFormatException e) {
            return faultValue;
        }
    }

    public static String[] breakLine(String line, Paint paint, int maxWidth) {
        // If playing activity not show up, maxWidth is invalid.
        if (line == null || maxWidth < 0) {
            String[] lineNullArray = {
                ""
            };
            return lineNullArray;
        }

        List<String> lines = new ArrayList<String>();
        int start = 0;
        int len = line.length();
        int lineCount = 1;

        // break
        int breakpoint = paint.breakText(line, start, len, true, maxWidth, null);

        while (start + breakpoint < len) {
            lineCount++;

            // if breakpoint is in a word, try to find a whitespace
            int wsIndex = line.lastIndexOf(' ', start + breakpoint);

            if (wsIndex > start) {
                lines.add(line.substring(start, wsIndex));
                start = wsIndex + 1;
            } else {
                lines.add(line.substring(start, start + breakpoint));
                start += breakpoint;
            }

            breakpoint = paint.breakText(line, start, len, true, maxWidth, null);
        }
        lines.add(line.substring(start, start + breakpoint));

        // convert to string array
        String[] lineArray = lines.toArray(new String[lineCount]);
        return lineArray;
    }
}
